package PrintAlternately;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-12-28
 * Time: 10:12
 */

public class TestDemo1 {
    private static final Object LOCK = new Object();

    //表示t2是否执行完
    private static boolean t2Running;

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            synchronized (LOCK) {
                while (!t2Running) {
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("1");
            }
        });
        t1.start();

        Thread t2 = new Thread(()->{
            synchronized (LOCK) {
                System.out.println("2");
                t2Running = true;
                LOCK.notifyAll();
            }
        });
        t2.start();
    }
}